(function ($) {
  'use strict';

  $(function () {
    var $fullText = $('.admin-fullText');
    $('#admin-fullscreen').on('click', function () {
      $.AMUI.fullscreen.toggle();
    });

    //琛ㄥ崟楠岃瘉
    if ($(".FormValidator")) bodyForm(".FormValidator");


    //寮€绁ㄨ祫鏂欏垏鎹�
    if ($("#invoice-toggle-box")) {
      $('.invoice-toggle-hide').on('click', function () {
        $("#invoice-toggle-box").addClass("am-hide");
      });
      $('.invoice-toggle-show').on('click', function () {
        $("#invoice-toggle-box").removeClass("am-hide");
      });
    }

  });
})(jQuery);



/* *
 * 琛ㄥ崟鍥炶皟
 */
function bodyForm(FormId) {
  $(FormId).validator({
    submit: function () {
      var formValidity = this.isFormValid();
      var postUrl = $(FormId).attr("action");
      $.when(formValidity).then(function (validity) {
        // 楠岃瘉鎴愬姛鐨勯€昏緫
        if (validity && postUrl) {
          var $button = $(FormId).find(".btn-loading-example");
          $button.button('loading');
          $.post(postUrl, $(FormId).serialize(), function (data) {
            if (data.status == "Y") {
              $button.button('reset');
              //鎻愮ず淇℃伅
              showAlert(FormId, "success", data.message, 2000);
              if (data.url) {
                window.location.href = data.url;
              }
            } else {
              showAlert(FormId, "danger", data.message, 2000);
            }
          }, "json")
        } else {
          //鎻愮ず淇℃伅
          showAlert(FormId, "danger", "璇锋鏌ュ～鍐欐牸寮忥紒", 2000);
        }
      }, function () {
        // 楠岃瘉澶辫触鐨勯€昏緫
        showAlert(FormId, "danger", "璇锋鏌ュ～鍐欐牸寮忥紒", 2000);
      });

      return false;
    }
  });
}


//鎻愮ず淇℃伅
function showAlert(odj, status, message, ms) {
  var $alert = $(odj).find(".am-alert");
  //鏄剧ず
  $alert.removeClass("am-hide").addClass("am-alert am-alert-" + status).find("p").html(message);
  //娓呴櫎楠岃瘉鐘舵€�
  if (status == "success") {
    $(odj).find(".am-form-success").removeClass("am-form-success");
    $(odj).find(".am-field-valid").removeClass("am-field-valid");
  }
  //寤舵椂闅愯棌
  setTimeout(function () {
    $alert.removeClass("am-alert-" + status).addClass("am-hide");
  }, ms);
}



//绯荤粺鎻愮ず
function sysDialog(data) {
  if (typeof data === 'object') {
    if (data.message) {
      var $html = '<div class="am-alert"><button type="button" class="am-close">&times;</button>' + data.message + '</div>';

      $('.theme-popover-mask').fadeIn(50);
      $('.theme-popover').html($html).show();
      var bh = $(".theme-popover").height();
      var bw = $(".theme-popover").width();
      $(".theme-popover").css({ "margin-left": (0 - bw / 2), "margin-top": (0 - bh / 2) });
      if (data.sleep) {
        setTimeout(function () { closeDialog(); }, data.sleep);
      } else {
        setTimeout(function () { closeDialog(); }, 1200);
      }
    } else {
      closeDialog();
    }
  }
}

//鍏抽棴鐏拌壊 jQuery 閬僵
function closeDialog() {
  $('.theme-popover-mask').fadeOut(50);
  $('.theme-popover').hide().html('<div class="load">姝ｅ湪鍔犺浇锛岃绋嶅悗....</div>');
  return false;
}

/* *
 * 閫夋嫨鍦板尯
 */
function getRegion(odj, level, selName) {

  var parent = $(odj).find("option:selected").val();
  var $this = $('#' + selName);
  if (parent && $this) {
    $.getJSON("../region.html", { parent: parent, level: level }, function (json) {
      if (json) {
        var html = '';
        $.each(json, function (i) {
          if (json[i] && json[i].length > 1) {
            html += '<option value="' + json[i][0] + '">' + json[i][1] + '</option>';
          }
        });
        $this.removeAttr("disabled").empty().append(html);
        if (level == 2) {
          var html = '<option value="">璇烽€夋嫨</option>';
          $('#selDistricts').empty().append(html).attr("disabled", "disabled");
        }
      } else {
        $this.attr("disabled", "disabled");
      }
    });
  }
  return false;
}

/* *
 * 娣诲姞鍟嗗搧鍒拌喘鐗╄溅 
 */
function addToCart(odj, code) {
  if (code) {
    var number = $(odj).val();
    $.getJSON('../carts/add.html', { qs: code, qty: number }, function (result) {
      if (result.status == "Y") {
        if (result.url) location.href = result.url;
        $('#ajaxCartNum').html(result.cartNum);
      }
      if (result.message) sysDialog(result);
    });
  }
  return false;
}


//璇㈢洏澶勭悊
function InquiryStatus(odj, act, id) {
  var $this = $(odj);
  var id = (id || $this.parents('tr').attr("rel"));

  if (act && id) {
    var url = $this.parents('.pageContent').attr("rel");
    $.ajax({
      type: 'POST', url: url, data: { id: id, act: act }, dataType: "json", cache: false,
      success: function (result) {
        if (result.status == "Y") {
          $this.parents(".am-panel").fadeOut("slow");
        } else {
          if (result.message) sysDialog(result);
        }
      }
    });
  }
  return false;
}